package com.ruoyi.web.controller.equipment;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.equipment.*;
import com.ruoyi.system.mapper.equipment.EquipmentBookMapper;
import com.ruoyi.system.mapper.equipment.EquipmentByzxMapper;
import com.ruoyi.system.mapper.equipment.EquipmentTypeMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IEquipmentByzxService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备保养执行Controller
 *
 * @author ruoyi
 * @date 2023-04-13
 */
@Api(tags = "设备保养执行api")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/equipment/byzx" )
public class EquipmentByzxController extends BaseController {

    private final IEquipmentByzxService iEquipmentByzxService;
    @Autowired
    private EquipmentBookMapper equipmentBookMapper;
    @Autowired
    private EquipmentTypeMapper equipmentTypeMapper;
    @Autowired
    private EquipmentByzxMapper equipmentByzxMapper;

    /**
     * 查询设备保养执行列表
     */
    @ApiOperation(value = "查询设备保养执行列表",response = EquipmentByzx.class)
    //@PreAuthorize("@ss.hasPermi('equipment:byzx:list')")
    @GetMapping("/list")
    public TableDataInfo list(EquipmentByzx equipmentByzx)
    {
        startPage();
        LambdaQueryWrapper<EquipmentByzx> lqw = new LambdaQueryWrapper<EquipmentByzx>();
        if (StringUtils.isNotBlank(equipmentByzx.getEquipmentName())){
            lqw.like(EquipmentByzx::getEquipmentName ,equipmentByzx.getEquipmentName());
        }
        if (equipmentByzx.getEquipmentId()!=null){
            lqw.like(EquipmentByzx::getEquipmentId ,equipmentByzx.getEquipmentId());
        }
        if (StringUtils.isNotBlank(equipmentByzx.getEquipmentType())){
            lqw.eq(EquipmentByzx::getEquipmentType ,equipmentByzx.getEquipmentType());
        }
        if (StringUtils.isNotBlank(equipmentByzx.getEquipmentCode())){
            lqw.eq(EquipmentByzx::getEquipmentCode ,equipmentByzx.getEquipmentCode());
        }
        if (equipmentByzx.getLineId() != null){
            lqw.eq(EquipmentByzx::getLineId ,equipmentByzx.getLineId());
        }
        if (StringUtils.isNotBlank(equipmentByzx.getLineName())){
            lqw.like(EquipmentByzx::getLineName ,equipmentByzx.getLineName());
        }
        if (equipmentByzx.getPlanDate() != null){
            lqw.eq(EquipmentByzx::getPlanDate ,equipmentByzx.getPlanDate());
        }
        if (StringUtils.isNotBlank(equipmentByzx.getByType())){
            lqw.eq(EquipmentByzx::getByType ,equipmentByzx.getByType());
        }
        if (StringUtils.isNotBlank(equipmentByzx.getByData())){
            lqw.eq(EquipmentByzx::getByData ,equipmentByzx.getByData());
        }
        if (StringUtils.isNotBlank(equipmentByzx.getByInfo())){
            lqw.eq(EquipmentByzx::getByInfo ,equipmentByzx.getByInfo());
        }
        if (equipmentByzx.getByDate() != null){
            lqw.eq(EquipmentByzx::getByDate ,equipmentByzx.getByDate());
        }
        if (equipmentByzx.getByStatus() != null){
            lqw.eq(EquipmentByzx::getByStatus ,equipmentByzx.getByStatus());
        }
        if (StringUtils.isNotBlank(equipmentByzx.getByNumber())){
            lqw.eq(EquipmentByzx::getByNumber ,equipmentByzx.getByNumber());
        }
        if (equipmentByzx.getByLx() != null){
            lqw.eq(EquipmentByzx::getByLx ,equipmentByzx.getByLx());
        }
        lqw.orderByDesc(EquipmentByzx::getCreateTime);
        List<EquipmentByzx> list = iEquipmentByzxService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出设备保养执行列表
     */
    //@PreAuthorize("@ss.hasPermi('equipment:byzx:export')" )
    @Log(title = "设备保养执行" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(EquipmentByzx equipmentByzx) {
        LambdaQueryWrapper<EquipmentByzx> lqw = new LambdaQueryWrapper<EquipmentByzx>(equipmentByzx);
        List<EquipmentByzx> list = iEquipmentByzxService.list(lqw);
        ExcelUtil<EquipmentByzx> util = new ExcelUtil<EquipmentByzx>(EquipmentByzx. class);
        return util.exportExcel(list, "byzx" );
    }

    /**
     * 获取设备保养执行详细信息
     */
    @ApiOperation(value = "获取设备保养执行详细信息")
    //@PreAuthorize("@ss.hasPermi('equipment:byzx:query')" )
    @GetMapping(value = "/{byzxId}" )
    public AjaxResult getInfo(@PathVariable("byzxId" ) Long byzxId) {
        EquipmentByzx eq = iEquipmentByzxService.getById(byzxId);
        EquipmentByzxDTO dto = new EquipmentByzxDTO();
        BeanUtil.copyProperties(eq,dto);
        if(StrUtil.isNotEmpty(eq.getImgPath())){
            dto.setImgPath(Arrays.asList(eq.getImgPath().split(",")));
        }
        return AjaxResult.success(dto);
    }


    @ApiOperation(value = "手动生成保养任务（后续删除）")
    @GetMapping(value = "/createByrw" )
    public AjaxResult createByrw() {
        List<EquipmentBook> list = equipmentBookMapper.selectList(new QueryWrapper<EquipmentBook>().eq("alert_state",1));
        list.stream().forEach(i->{
//            EquipmentByzx equipmentByzx = new EquipmentByzx();
//            equipmentByzx.setEquipmentCode(i.getBookCode());
//            equipmentByzx.setEquipmentName(i.getBookName());
//
//            EquipmentType equipmentType = equipmentTypeMapper.selectById(i.getTypeId());
//
//            equipmentByzx.setEquipmentType(String.valueOf(equipmentType.getTypeId()));
//            equipmentByzx.setEquipmentTypeName(equipmentType.getTypeName());
//            equipmentByzx.setPlanDate(i.getApplyDate());
//            equipmentByzx.setByData("校准");
//            equipmentByzx.setByType("校准");
//            equipmentByzx.setEquipmentId(i.getBookId());
//            equipmentByzx.setByLx(1);
//            equipmentByzxMapper.insert(equipmentByzx);
        });
        return AjaxResult.success();
    }


    /**
     * 新增设备保养执行
     */
    @ApiOperation(value = "新增设备保养执行")
    //@PreAuthorize("@ss.hasPermi('equipment:byzx:add')" )
    @Log(title = "设备保养执行" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EquipmentByzxDTO dto) {

        EquipmentByzx equipmentByzx = new EquipmentByzx();
        BeanUtil.copyProperties(dto,equipmentByzx);
        String join = StringUtils.join(dto.getImgPath(), ",");
        equipmentByzx.setImgPath(join);
        equipmentByzx.setCreateBy(SecurityUtils.getUsername());
        equipmentByzx.setCreateTime(new Date());

        //根据设备id获取设备名称
        EquipmentBook equipmentBook = equipmentBookMapper.selectById(dto.getEquipmentId());
        equipmentByzx.setEquipmentCode(equipmentBook.getBookCode());
        equipmentByzx.setEquipmentName(equipmentBook.getBookName());
        //根据设备类型id获取设备类型名称
        EquipmentType equipmentType = equipmentTypeMapper.selectById(dto.getEquipmentType());
        dto.setEquipmentType(equipmentType.getTypeName());

        return toAjax(iEquipmentByzxService.save(equipmentByzx) ? 1 : 0);
    }

    /**
     * 修改设备保养执行
     */
    @ApiOperation(value = "修改设备保养执行")
    //@PreAuthorize("@ss.hasPermi('equipment:byzx:edit')" )
    @Log(title = "设备保养执行" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EquipmentByzxDTO dto) {
        AjaxResult ajaxResult = iEquipmentByzxService.byzx(dto);
        return ajaxResult;
    }

    /**
     * 删除设备保养执行
     */
    @ApiOperation(value = "删除设备保养执行")
    //@PreAuthorize("@ss.hasPermi('equipment:byzx:remove')" )
    @Log(title = "设备保养执行" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{byzxIds}" )
    public AjaxResult remove(@PathVariable Long[] byzxIds) {
        return toAjax(iEquipmentByzxService.removeByIds(Arrays.asList(byzxIds)) ? 1 : 0);
    }
}
